package com.qyer.util.log.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.qyer.util.log.util.HttpRequest;
import com.qyer.util.log.util.JsonUtil;

public class LatLon {
	
	
	static String url ="http://open.qyer.com/common/neighboring_poi_list"; 
	
	static String param111="client_id=RA_DATA&client_secret=18f27aa9ad51e7d59b86&limit=1&distance=10000&order_type=1";

	public static void main(String[] args) {
		getMapApp();
	}
	
	public static Map<String , String > getMapApp(){
		Map<String , String > ugc_typeMaps = new HashMap<String,String>();
		
		String fileName="/Users/sunhongxi/Documents/sshfileupdown/tmp_20151201_1.txt";

		File file = new File(fileName);
        BufferedReader reader = null;
        Map<String , Integer> ms = new HashMap<String ,Integer>();
        Set<String> lms = new HashSet<String>();
        List <String> ls = new ArrayList <String>();
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            long s1= System.currentTimeMillis(); 
            String lat =null ; 
            String lng =null ; 
            String tid =null ; 
            String tmplat=null; 
            String tmplng=null; 
            
            int i=0 ,x =0 ;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
            	i++;
            	String []ts =tempString.split("\t");
            	if(ts.length<6){
            		continue; 
            	}
//            	tid=ts[2];
            	lat =ts[4]; 
            	lng =ts[5];
            	
            	try {
					tmplat=lat.substring( 0,lat.indexOf(".")+7 );
					tmplng=lng.substring(0,lng.indexOf(".")+ 7 );
				} catch (Exception e) {
					tmplat=lat ; 
					tmplng=lng; 
				}
            	
            	if(lms.contains("["+tmplat+","+tmplng+"]")){
            		ls.add("["+tmplat+","+tmplng+"]");
            	}else{
            		String param="lat="+lat+"&lng="+lng+"&"+param111;
                	String res =HttpRequest.sendGet(url, param);
            		
            		//System.out.println(res);
            		
            		Object dataStr=JsonUtil.jsonToMap(res).get("data");
            		//System.out.println(dataStr);
            		String []fs = dataStr.toString().split(",");
            		
            		Map <String ,String> maps = new HashMap<String ,String>();
            		
            		for(String f : fs ){
            			if(f.split("=").length>1){
            				String key=f.split("=")[0];
            				String value=f.split("=")[1];
            				key=key.trim();
            				value=value.trim();
            				maps.put(key, value);
            			}
            		}
                	if ("8845".equals(maps.get("cityid"))){
                		try {
    						lat=lat.substring( 0,lat.indexOf(".")+7 );
    						lng=lng.substring(0,lng.indexOf(".")+ 7 );
    					} catch (Exception e) {
    						lat=lat ; 
    						lng=lng; 
    					}
                		System.out.println("i+=="+i+"===   ["+lat+","+lng+"]");
                		ls.add("["+lat+","+lng+"]");
                		lms.add("["+lat+","+lng+"]");
                	}
            	}
        		
        		x++;
            }
            reader.close();
            System.out.println("i ===="+i);
            System.out.println("x ===="+x);
            System.out.println(ls.toString());
            System.out.println(ls.size());
            
            file = new File("/Users/sunhongxi/Documents/sshfileupdown/res_tmp_20151201_1.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
             file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("[");
            int ii=0 ;
            for(String s : ls){
            	ii ++;
            	if(ii==ls.size()){
            		bw.write(s);
            	}else{
            		bw.write(s+",\n");
            	}
            }
            bw.write("]");
            bw.close();

            System.out.println("Done");
            
            return ugc_typeMaps;
        } catch (IOException e) {
            e.printStackTrace();
            return null; 
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
	}

}
